<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'nochecked'=>0,
		'no_showsys'=>0,
		'type <>'=>4
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	//有关键词时，分类ID也检索！
	if(($_RQ['pcate'] || $_RQ['pid']) && $_RQ['pid']!=-1){
			$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
			$params['pcate']=$pcate;
			$return['params']['pcate']=$pcate;
	}
	if($_RQ['ccate'] || $_RQ['cid']){
			$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
			$params['ccate']=$ccate;
			$return['params']['ccate']=$ccate;
	}
	if($_RQ['scate'] || $_RQ['sid']){
			$scate=$_RQ['scate']?intval($_RQ['scate']):intval($_RQ['sid']);
			$params['scate']=$scate;
			$return['params']['scate']=$scate;
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}elseif($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}elseif($_RQ['order']=='ishot' && !$_RQ['ccate'] && !$_RQ['pcate'] && !$_RQ['scate']){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}else{
		$return['params']['order']=$_RQ['order'];
	}
	if($_RQ['priceMin'] && $_RQ['priceMax']){
		$params['price between']=array(intval($_RQ['priceMin']),intval($_RQ['priceMax']));
	}elseif($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}elseif($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	if($_RQ['isvideogoods'] || $_RQ['order']=='isvideogoods'){
		$params['video <>']='';
		$return['params']['isvideogoods']=intval($_RQ['isvideogoods']);
	}
	if($_RQ['star']){
		$params['star >=']=intval($_RQ['star']);
		$return['params']['star']=intval($_RQ['star']);
	}
	//参数搜索
	foreach($_RQ as $key=>$rv){
		if(strpos($key,'params') !== false && $rv){
			$params['param like']='%'.trim($rv).'%';
		}
	}
	//电商客商品
	if($_RQ['isweishopke']){
		$params['pluginname']=array('jy_taobaoke','jy_jdk','jy_pddke');
		$return['params']['isweishopke']=1;
	}else{
		$onepluginname=model('jy_weishop/goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
		if($onepluginname){//开启独立商品
				$params['onepluginname OR']='merchid > \'0\' or onepluginname=\''.$onepluginname.'\'';
				$return['params']['onepluginname']=$onepluginname;
		}else{
				$params['onepluginname']='';
		}
	}
	//插件关联：
	if(trim($_RQ['pluginname'])){
		$params['pluginname']=trim($_RQ['pluginname']);
		unset($params['onepluginname']);
		$return['params']['pluginname']=trim($_RQ['pluginname']);
	}
	if(!$params['pluginname']){
		$params['pluginname !=']=array('jy_taobaoke','jy_jdk','jy_pddke');
	}
	if($return['shopset']['no_showmerch']==1){
		$params['merchid']=0;
	}
	if($_RQ['merchid']){
		unset($params['nochecked']);
		unset($params['no_showsys']);
		unset($params['onepluginname']);
		$params['merchid']=$_RQ['merchid'];
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_SESSION['os']=='windows'?12:10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','api_data');
	if($_RQ['wxappjiaoyi']){
		$fields[]='api_id';
		$params['api_id >']=0;
		$params['api_pluginname']='jy_wxappjiaoyib';
	}else{
		$params['api_pluginname <>']='jy_merch';
	}
	$orderby='paixu desc, star desc,  salenum desc, id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc, paixu desc';
		}
		if($_RQ['order']=='new'){
			$orderby='id desc, paixu desc';
		}
		if($_RQ['order']=='star'){
			$orderby='star desc, paixu desc';
		}
		if($_RQ['order']=='you'){
			$orderby='salenum desc, paixu desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
		}
	}
	if(($_RQ['order']=='upvip' || $_RQ['order']=='sale_limit' || $_RQ['order']=='sale_presell'  || $_RQ['order']=='sale_cuxiao' || $_RQ['order']=='sale_offer' || $_RQ['order']=='sale_new' || $_RQ['order']=='sale_libao') && !$return['params']['pcate'] && !$return['params']['ccate']){
		//if(intval($_RQ['page'])==1){
			if( $_RQ['order']=='sale_offer'){
				$getoffer=model('jy_weishop/activity/sales')->getoffer();
				$list=$getoffer['list'];
			}
			if( $_RQ['order']=='sale_cuxiao'){
				$list=model('jy_weishop/activity/sales')->goods_list(500);
			}
			if( $_RQ['order']=='sale_limit'){
				$list=model('jy_weishop/activity/limit')->goods_list(500);
			}
			if( $_RQ['order']=='sale_presell'){
				$list=model('jy_weishop/activity/presell')->goods_list(500);
			}
			if( $_RQ['order']=='sale_new'){
				$sale_new=model('jy_weishop/activity/sales')->getnew();
				$list=$sale_new['list'];
			}
			if( $_RQ['order']=='sale_libao' && $_RQ['sale_libaoid']){
				$return['params']['sale_libaoid']=$_RQ['sale_libaoid'];
				$sale_libao=model('jy_weishop/activity/sales')->getlibao($_RQ['sale_libaoid']);
				$list=$sale_libao['list'];
			}
			if($_RQ['order']=='upvip'){
				$nextlevel=model('jy_weishop/member')->get_nextlevel();
				$nextlevel['goods_ids']=json_decode($nextlevel['goods_ids'],true);
				if($nextlevel['up_type']!=2){
					if(count($nextlevel['goods_ids'])){
						$list=$nextlevel['goods_ids'];
					}else{
						$isnogoods_ids=true;
					}
				}else{
					$isnogoods_ids=true;
				}
			}
			$ids=array();
			foreach($list as &$ov){
				$ids[$ov['id']]=$ov['id'];
			}
			if(count($ids)>0){
				$ids=array_keys($ids);
			}else{
				$ids=array(0);
			}
			if(!$isnogoods_ids){
				$params['id']=$ids;
			}
			$parameter=array(
				'params'=>$params,
				'limit'=>array($pindex,$psize),
			);
			$list=model('jy_weishop/goods')->goods_list($parameter,$total);
		//}
	}else{
		$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
		);
		$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	}
	//print_r($params);exit;
	if(empty($list)){
		exi('none');
	}else{
		foreach($list as $k=>&$itemv){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$itemv['id']));
			$api_data=json_decode($goods['api_data'],true);
			$itemv['leixings']=$api_data['leixings']?:array();
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['jianprice']=$itemv['originalprice']-$itemv['price'];
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			if($itemv['merchid'] && $this->plugin_exists('jy_merch')){
				$merch=pdo_get('jy_merch_user',array('id'=>$itemv['merchid']),array('status','merchname'));
				if($merch['status']==2 && $itemv['merchid']>0){
					pdo_update('jy_weishop_goods',array('nochecked'=>1),array('merchid'=>$itemv['merchid'],'uniacid'=>$_SESSION['uniacid']));
					unset($list[$k]);
					continue;
				}
				$itemv['merchname']=$merch['merchname'];
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$api_data=json_decode($itemv['api_data'],true);
				if($api_data['istaocan']){
					$specs_list=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>''));
					$selespename = $specs_list[0]['title'];
					$itemv['istaocan'] = 1;
				}else{
					$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($specs_list){
							foreach($specs_list as $k=>&$sp){
								$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							}
					}
					//已选规格
					$selectspecs = array();
					$selespename = '';
					$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
					$selectspecs = str_array($specs,'_');
					if($selectspecs){
							foreach($selectspecs as $k=>&$spid){
								$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
								$selespename .=$title.'，';
							}
					}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
			if($_RQ['wxappjiaoyi']){
				$itemv['id']=$itemv['api_id'];
			}
		}
	}
	$list=array_values($list);
	if(empty($list)){
		exi('none');
	}else{
		exi($list);
	}
}
?>